package com.zzyl.controller.security;

import com.zzyl.base.PageBean;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.PasswordDto;
import com.zzyl.dto.UserDto;
import com.zzyl.service.UserService;
import com.zzyl.vo.UserRoleVo;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户分页
     *
     * @param dto      用户Dto
     * @param pageNum  书籍页码
     * @param pageSize 页面大小
     * @return {@code ResponseResult<PageBean<UserVo>> }
     * @author YiSheep
     */
    @ApiOperation("用户分页")
    @PostMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult<PageBean<UserVo>> pageQuery(@RequestBody UserDto dto, @PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        log.info("用户分页 dto:{},pageNum:{},pageSize:{}", dto, pageNum, pageSize);
        PageBean<UserVo> pageBean = userService.pageQuery(dto, pageNum, pageSize);
        return ResponseResult.success(pageBean);
    }

    /**
     * 用户添加
     *
     * @param dto 用户Dto
     * @return {@code ResponseResult<Void> }
     * @author YiSheep
     */
    @ApiOperation("用户添加")
    @PutMapping
    public ResponseResult<Void> userAdd(@RequestBody UserDto dto) {
        log.info("用户添加 dto:{}", dto);
        userService.userAdd(dto);
        return ResponseResult.success();
    }

    /**
     * 用户修改
     *
     * @param dto 用户Dto
     * @return {@code ResponseResult<Void> }
     * @author YiSheep
     */
    @ApiOperation("用户修改")
    @PatchMapping
    public ResponseResult<Void> userUpdate(@RequestBody UserDto dto) {
        log.info("用户修改 dto:{}", dto);
        userService.userUpdate(dto);
        return ResponseResult.success();
    }

    /**
     * 删除用户
     *
     * @param userIds 用户ID
     * @return {@code ResponseResult<Void> }
     * @author YiSheep
     */
    @ApiOperation("删除用户")
    @DeleteMapping("/remove/{userIds}")
    public ResponseResult<Void> userDelete(@PathVariable Long userIds) {
        log.info("删除用户 userIds:{}", userIds);
        userService.userDelete(userIds);
        return ResponseResult.success();
    }

    /**
     * 启用或禁用用户
     *
     * @param id     用户ID
     * @param status 状态
     * @return {@code ResponseResult<Void> }
     * @author YiSheep
     */
    @ApiOperation("启用或禁用用户")
    @PutMapping("/is-enable/{id}/{status}")
    public ResponseResult<Void> enableOrDisable(@PathVariable(name = "id") Long id,
                                         @PathVariable(name = "status") String status) {
        log.info("启用或禁用用户 id:{},status:{}", id, status);
        userService.enableOrDisable(id, status);
        return ResponseResult.success();
    }


    /**
     * 用户列表
     *
     * @param dto 用户Dto
     * @return {@code ResponseResult<List<UserVo>> }
     * @author YiSheep
     */
    @ApiOperation("用户列表")
    @PostMapping("/list")
    public ResponseResult<List<UserVo>> list(@RequestBody UserDto dto) {
        log.info("用户列表 dto:{}", dto);
        List<UserVo> userVoList = userService.list(dto);
        return ResponseResult.success(userVoList);
    }

    /**
     * 密码重置
     *
     * @param userId 用户id
     * @return {@code ResponseResult<Void> }
     * @author YiSheep
     */
    @ApiOperation("密码重置")
    @PostMapping("/reset-passwords/{userId}")
    public ResponseResult<Void> resetPasswords(@PathVariable Long userId) {
        log.info("密码重置 userId:{}", userId);
        userService.resetPasswords(userId);
        return ResponseResult.success();
    }

    /**
     * 密码校验
     *
     * @param dto 密码dto
     * @return {@code ResponseResult<Void> }
     * @author YiSheep
     */
    @ApiOperation("密码校验")
    @PutMapping("/cmodify-passwords")
    public ResponseResult<Void> cmodifyPasswords(@RequestBody PasswordDto dto) {
        log.info("密码校验 dto:{}", dto);
        userService.cmodifyPasswords(dto);
        return ResponseResult.success();
    }

    /**
     * 修改密码
     *
     * @param dto 密码dto
     * @return {@code ResponseResult<Void> }
     * @author YiSheep
     */
    @ApiOperation("修改密码")
    @PutMapping("/modify-passwords")
    public ResponseResult<Void> modifyPasswords(@RequestBody PasswordDto dto) {
        log.info("修改密码 dto:{}", dto);
        userService.modifyPasswords(dto);
        return ResponseResult.success();
    }

    /**
     * 个人信息
     *
     * @return {@code ResponseResult<UserVo> }
     * @author YiSheep
     */
    @GetMapping("/current-user")
    @ApiOperation(value = "个人信息", notes = "个人信息")
    public ResponseResult<UserVo> getCurrentUser() {
        log.info("个人信息");
        UserVo userVo = userService.getCurrentUser();
        return ResponseResult.success(userVo);
    }
}
